<?php

/**
 * This is the model class for table "business".
 *
 * The followings are the available columns in table 'business':
 * @property integer $business_id
 * @property string $business_name
 * @property string $opening_hours
 * @property string $tel_no
 * @property string $mobile_no
 * @property string $qr_code
 * @property string $business_logo
 * @property string $tag_line
 * @property string $address
 * @property string $business_type
 * @property string $email
 * @property string $business_site_stats
 * @property string $online_menu_url
 * @property string $home_url
 *
 * The followings are the available model relations:
 * @property Account[] $accounts
 * @property Subscription[] $subscriptions
 */
class Business extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Business the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'business';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('business_name, tel_no, mobile_no, address, business_type', 'required'),
			array('business_name, business_site_stats', 'length', 'max'=>50),
			array('tel_no', 'length', 'max'=>20),
			array('mobile_no, business_type, email', 'length', 'max'=>30),
			array('tag_line, address, online_menu_url', 'length', 'max'=>100),
			array('home_url', 'length', 'max'=>150),
			array('opening_hours', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('business_id, business_name, opening_hours, tel_no, mobile_no, qr_code, business_logo, tag_line, address, business_type, email, business_site_stats, online_menu_url, home_url', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'accounts' => array(self::HAS_MANY, 'Account', 'business_id'),
            'offers' => array(self::HAS_MANY, 'Offer', 'business_id'),
			'subscriptions' => array(self::HAS_MANY, 'Subscription', 'business_id'),
		);
	}
	
	 /**
     * @return override the CActiveRecord::defaultScope method, so that only display user's own record.
     * id used here is the business id stored in account table
     */
    public function defaultScope()
    {
        return array(
            'condition'=>'business_id='.Yii::app()->user->id,
        );
    }

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'business_id' => 'Business',
			'business_name' => 'Business Name',
			'opening_hours' => 'Opening Hours',
			'tel_no' => 'Tel No',
			'mobile_no' => 'Mobile No',
			'qr_code' => 'Qr Code',
			'business_logo' => 'Business Logo',
			'tag_line' => 'Tag Line',
			'address' => 'Address',
			'business_type' => 'Business Type',
			'email' => 'Email',
			'business_site_stats' => 'Business Site Stats',
			'online_menu_url' => 'Online Menu Url',
			'home_url' => 'Home Url',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('business_id',$this->business_id);
		$criteria->compare('business_name',$this->business_name,true);
		$criteria->compare('opening_hours',$this->opening_hours,true);
		$criteria->compare('tel_no',$this->tel_no,true);
		$criteria->compare('mobile_no',$this->mobile_no,true);
		$criteria->compare('qr_code',$this->qr_code,true);
		$criteria->compare('business_logo',$this->business_logo,true);
		$criteria->compare('tag_line',$this->tag_line,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('business_type',$this->business_type,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('business_site_stats',$this->business_site_stats,true);
		$criteria->compare('online_menu_url',$this->online_menu_url,true);
		$criteria->compare('home_url',$this->home_url,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}